package com.winit.openapi.ims.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.ims.spi.SingleItemService;
import com.winit.ims.spi.singleItem.command.GenerateSingleItemLabelsByMCodeCommand;
import com.winit.ims.spi.singleItem.command.GenerateSingleItemLabelsBySCodeCommand;
import com.winit.ims.spi.singleItem.command.GenerateSingleItemLabelsCommand;
import com.winit.ims.spi.singleItem.vo.GenerateSingleItemLabelsRespVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

@Controller
@RequestMapping(value = "/ims/item")
public class SingleItemController extends BaseController {

    @Resource
    private SingleItemService singleItemService;
    @Value(value = "${url.erp}")
    private String            erpUrl;

    /**
     * 创建单品标签 add by zhouxq， 2016-7-8
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabelsNoMerged")
    public String generateSingleItemLabelsNoMerged(@ModelAttribute RequestMsg requestMsg,
                                                   @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);

        GenerateSingleItemLabelsCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        List<GenerateSingleItemLabelsRespVo> respVos = singleItemService.generateSingleItemLabels(command);
        responseMsg.setData(respVos);
        return SUCCESS;
    }

    /**
     * 创建单品标签 add by zhouxq， 2016-11-25
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabels")
    public String generateSingleItemLabels(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);

        GenerateSingleItemLabelsCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        String label = singleItemService.generateSingleItemLabelsNew(command);
        responseMsg.setData(label);
        return SUCCESS;
    }

    /**
     * 创建单品标签 add by zhouxq， 2016-11-18
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabelsBySCode")
    public String generateSingleItemLabelsBySCode(@ModelAttribute RequestMsg requestMsg,
                                                  @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);

        GenerateSingleItemLabelsBySCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsBySCodeCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        String label = singleItemService.generateSingleItemLabelsBySCode(command);
        responseMsg.setData(label);
        return SUCCESS;
    }

    /**
     * 创建单品标签 add by zhouxq， 2016-7-8
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabelsMerged")
    public String generateSingleItemLabelsMerged(@ModelAttribute RequestMsg requestMsg,
                                                 @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        // 获取客户编码
        // QueryCompanylCommand userCommand = new QueryCompanylCommand();
        // userCommand.setEmail(CommandContext.getContext().getUsername());
        // CustomerInfoVO customer =
        // umsCustomerService.queryCompanyByUserName(userCommand);
        // if (customer == null || customer.getCode() == null) {
        // throw new ApiException(ErrorCode.USER_NOT_FOUND,
        // CommandContext.getContext().getUsername());
        // }

        GenerateSingleItemLabelsCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        String label = singleItemService.generateSingleItemLabelsMerged(command);
        responseMsg.setData(label);
        return SUCCESS;
    }

    /**
     * 创建单品标签 add by zhouxq， 2016-7-8
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabelsByMCode")
    public String generateSingleItemLabelsByMCode(@ModelAttribute RequestMsg requestMsg,
                                                  @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        GenerateSingleItemLabelsByMCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsByMCodeCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        String label = singleItemService.generateSingleItemLabelsByMCode(command);
        responseMsg.setData(label);
        return SUCCESS;
    }

    /**
     * 创建单品标签 add by zhouxq， 2016-7-8
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabelsUrlByMCode")
    public String generateSingleItemLabelsUrlByMCode(@ModelAttribute RequestMsg requestMsg,
                                                     @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        GenerateSingleItemLabelsByMCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsByMCodeCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        String labelPath = singleItemService.generateSingleItemLabelsUrlByMCode(command);
        responseMsg.setData(labelPath);
        return SUCCESS;
    }

    /**
     * 创建单品标签 add by zhouxq， 2016-11-21
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/generateSingleItemLabelsUrlByMCodeLarge")
    public String generateSingleItemLabelsUrlByMCodeLarge(@ModelAttribute RequestMsg requestMsg,
                                                          @ModelAttribute ResponseMsg responseMsg) {

        CommandContext ctx = CommandContext.getContext();
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        GenerateSingleItemLabelsByMCodeCommand command = JSON.parseObject(requestMsg.getData().toString(),
            GenerateSingleItemLabelsByMCodeCommand.class);
        command.setCustomerCode(bpartner.getValue());
        command.setCtx(ctx);
        String labelPath = singleItemService.generateSingleItemLabelsUrlByMCodeLarge(command);
        responseMsg.setData(labelPath);
        return SUCCESS;
    }

}
